import axios from 'axios'
import { message } from 'antd';
// export const serverUrl = '/'
export const serverUrl = 'http://localhost:8080'
const instance = axios.create({
    timeout: 5000,
    baseURL: serverUrl,
    headers: {
        "Content-Type": "application/json",
        // 'Content-Type': 'application/x-www-form-urlencoded',//会使数组提交格式变为'key[]':[]
    },
})
// 添加请求拦截器
instance.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    config.headers.token = localStorage.getItem('token') || ''
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
instance.interceptors.response.use(function (response) {
    // 2xx 范围内的状态码都会触发该函数。
    // 对响应数据做点什么
    return response.data;
}, function (error) {
    console.log('error',error);
    if (error.status === 403) {
        message.error('Token 已过期, 请重新登录')
    }
    if (error.code === 'ERR_NETWORK') {
        message.error('服务器未响应')
    }
    return Promise.reject(error);
});
export const get: any = (url: string, params: any) => instance.get(url, { params })
export const post = (url: string, data: any) => instance.post(url, data)
export const put = (url: string, data: any) => instance.put(url, data)
export const patch = (url: string, data: any) => instance.patch(url, data)
export const del = (url: string) => instance.delete(url)
